package v1

import (
	"yuanxin-admin/internal/common/ioc"
	"yuanxin-admin/internal/dto"
	"yuanxin-admin/internal/model"
	"yuanxin-admin/internal/service"

	"gitee.com/botaoshow/yuanxin/web/response"
	"gitee.com/botaoshow/yuanxin/web/utils/strs"
	"github.com/gin-gonic/gin"
)

type SysDeptController struct {
	sysDeptService service.ISysDeptService
}

func NewSysDeptController() *SysDeptController {
	c := &SysDeptController{
		sysDeptService: ioc.Inject[service.ISysDeptService]((service.ISysDeptService)(nil)),
	}
	return c
}

func (s *SysDeptController) Save(c *gin.Context) {
	var d dto.SysDeptDTO
	if err := c.ShouldBind(&d); err != nil {
		response.Fail(c, err.Error())
		return
	}
	var entity = &model.SysDept{
		ID:       strs.GetUUIDWithoutDashUpper(),
		DeptName: d.DeptName,
		ParentID: d.ParentId,
		SortNum:  d.SortNum,
	}
	if flag, err := s.sysDeptService.Save(c, entity); err != nil {
		response.Fail(c, err.Error())
	} else {
		response.ToAjax(c, flag)
	}

}

func (s *SysDeptController) Update(c *gin.Context) {
	id := c.Param("id")
	var d dto.SysDeptDTO
	if err := c.ShouldBind(&d); err != nil {
		response.Fail(c, err.Error())
		return
	}
	var entity = &model.SysDept{
		ID:       id,
		DeptName: d.DeptName,
		ParentID: d.ParentId,
		SortNum:  d.SortNum,
	}

	if flag, err := s.sysDeptService.Update(c, entity); err != nil {
		response.Fail(c, err.Error())
	} else {
		response.ToAjax(c, flag)
	}

}

func (s *SysDeptController) Remove(c *gin.Context) {
	id := c.Param("id")
	if flag, err := s.sysDeptService.Remove(c, id); err != nil {
		response.Fail(c, err.Error())
	} else {
		response.ToAjax(c, flag)
	}
}

func (s *SysDeptController) List(c *gin.Context) {
	if list, err := s.sysDeptService.ListDept(c); err != nil {
		response.Fail(c, err.Error())
		return
	} else {
		response.Success(c, list)
	}

}
